routeToLocation.js ➔ ... ➔ ???   B
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 4
c 3
b 1
f 0
nc 1
dl 0
loc 25
rs 8.5806
nop 1
1
import pathToRegexp from 'path-to-regexp';
2
import { stringify as qsStringify } from 'query-string';
3
import { createPath } from 'history';
4
import { flattenRoutes } from './util';
5
import RouterError from '../error';
6
7
const ERRORS = {
8
    noId: _ => 'Can\'t match route with no id',
0 ignored issues
show
Unused Code introduced by
The parameter _ is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
9
    notFound: id => `Route with id ${id} not found`
10
};
11
12
const createMatchRouteToPath = registry => ({ id, params = {}, query = {}, hash = ''}) => {
13
    if (id === undefined) throw new RouterError(ERRORS.noId());
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
14
15
    const matcher = registry[id];
16
17
    if (matcher === undefined) throw new RouterError(ERRORS.notFound(id));
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
18
19
    let pathname;
20
21
    try {
22
        // path-to-regexp (1.6.0): encodeURI by default, disable it with decodeURI
23
        // 'pretty' flag disable all encoding, besides '/', '?', '#'
24
        pathname = decodeURIComponent(matcher(params));
25
    } catch (e) {
26
        throw new RouterError(e.toString());
27
    }
28
29
    const location = {
30
        search: qsStringify(query),
31
        pathname,
32
        hash
33
    };
34
35
    return createPath(location);
36
};
37
38
const createRouteToLocationParser = routes => {
39
40
    const registry = flattenRoutes(routes).reduce((result, item) => {
41
        if (result[item.id]) {
42
            return result;
43
        }
44
        result[item.id] = pathToRegexp.compile(item.pattern.path);
45
        return result;
46
    }, {});
47
48
    return createMatchRouteToPath(registry);
49
};
50
51
export default createRouteToLocationParser;